package visitor.outputvisitor

import ast.expr.SqlIdentifierExpr
import ast.expr.SqlPropertyExpr
import ast.limit.SqlLimit
import ast.statement.upsert.SqlUpsert

class PgsqlOutPutVisitor : SqlOutPutVisitor() {
    override fun visitSqlLimit(sqlLimit: SqlLimit) {
        sqlBuilder.append("LIMIT ${sqlLimit.limit} OFFSET ${sqlLimit.offset}")
    }

    override fun visitSqlUpsert(sqlUpsert: SqlUpsert) {
        sqlBuilder.append("INSERT INTO ")
        visitSqlExpr(sqlUpsert.table!!)

        sqlBuilder.append(" (")
        printList(sqlUpsert.columns, ::visitSqlExpr)
        sqlBuilder.append(")")

        sqlBuilder.append(" VALUES")
        sqlBuilder.append(" (")
        printList(sqlUpsert.value, ::visitSqlExpr)
        sqlBuilder.append(")")

        sqlBuilder.append(" ON CONFLICT")
        sqlBuilder.append(" (")
        printList(sqlUpsert.primaryColumns, ::visitSqlExpr)
        sqlBuilder.append(")")

        sqlBuilder.append(" DO UPDATE SET ")

        printList(sqlUpsert.updateColumns) {
            visitSqlExpr(it)
            sqlBuilder.append(" = ")
            sqlBuilder.append("EXCLUDED.")
            visitSqlExpr(it)
        }
    }
}